<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '管理员添加' : '管理员修改'"
      :visible.sync="info.isshow"
      width="50%"
    >
      <div>form:{{ form }}</div>
      <el-form :model="form" label-width="80px">
        <el-form-item label="所属角色">
          <el-select v-model="form.roleid" placeholder="请选择地址">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
        <el-button type="primary" v-if="info.isAdd" @click="Add"
          >添加</el-button
        >
        <el-button type="primary" v-else @click="edit">修改</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqrolelist,requseradd,requseredit,requserinfo, } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      roleList: [],
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      userInfo:"userInfo"
    }),
  },

  mounted() {
    //请求角色数据
    reqrolelist().then((res) => {
      if (res.data.code == 200) {
        this.roleList = res.data.list ? res.data.list : [];
      }
    });
  },
  methods: {
    ...mapActions({
      changeUserInfo:"changeUserInfo"
    }),
    empty() {
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    Add() {
      requseradd(this.form).then((res) => {
        //获取到数据之后，要弹成功，要渲染页面，要重新请求，要关闭页面
        successAlert(res.data.msg);
        this.empty();
        this.info.isshow = false;
        this.$emit("init");
      });
    },
    getOne(id) {
      requserinfo({ uid: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.password = "";
        }
      });
    },
     edit() {
      requseredit(this.form).then((res) => {
        if (res.data.code === 200) {
          successAlert(res.data.msg);

           if(this.form.uid==this.userInfo.uid){
            this.changeUserInfo({})
            this.$router.push("/login")
            return;
          }

          this.empty();
          this.info.isshow = false;
          this.$emit("init");
        }
      });
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>